package com.neutech.dao.impl;

import com.neutech.constant.Constant;
import com.neutech.dao.BusinessDAO;
import com.neutech.entity.Business;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessDAOImpl implements BusinessDAO {
    @Override
    public Business getById(Integer id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet =null;
        Business business = null;
        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql ="select * from e_business where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                business = new Business();
                business.setId(resultSet.getInt("id"));
                business.setName(resultSet.getString("name"));
                business.setAddress(resultSet.getString("address"));
                business.setDescription(resultSet.getString("description"));
                business.setMainImage(resultSet.getString("main_image"));
                business.setMonthSales(resultSet.getInt("month_sales"));
                business.setStartPrice(resultSet.getBigDecimal("start_price"));
                business.setDeliveryPrice(resultSet.getBigDecimal("delivery_price"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (resultSet !=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return business;
    }

    @Override
    public List<Business> listBusiness() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet =null;
        List<Business> businessList = new ArrayList<>();
        try {
            connection = Constant.DATASOURCE.getConnection();
            preparedStatement = connection.prepareStatement("select * from e_business");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Business business = new Business();
                business.setId(resultSet.getInt("id"));
                business.setName(resultSet.getString("name"));
                business.setAddress(resultSet.getString("address"));
                business.setDescription(resultSet.getString("description"));
                business.setMainImage(resultSet.getString("main_image"));
                business.setMonthSales(resultSet.getInt("month_sales"));
                business.setStartPrice(resultSet.getBigDecimal("start_price"));
                business.setDeliveryPrice(resultSet.getBigDecimal("delivery_price"));
                businessList.add(business);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (connection != null){
                try {
                   connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (resultSet !=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return businessList;
    }
}
